/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * BuscaMotorista.java
 *
 * Created on 02/08asfdasdfa/2011, 23:14:46
 */

package View.Seguro;

import Controle.Controlador;
import Controle.Uteis;
import Modelo.Cliente;
import Modelo.Cliente_has_Seguro;
import Modelo.Fisico;
import Modelo.Juridico;
import Modelo.Seguro;
import View.Fundo2;
import View.Iniciar;
import View.IniciarFuncionario;
import View.Progress;
import View.TelaMetodos;
import java.awt.BorderLayout;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Fernando
 */
public class BuscaSeguroVisualizar extends javax.swing.JFrame {
public static Fundo2 pnlFundo = new Fundo2();

    /** Creates new form BuscaMotorista */
    public BuscaSeguroVisualizar() {
         initComponents();

    this.pack();
    this.setLayout(new BorderLayout());
      this.add(pnlFundo, BorderLayout.CENTER);
    this.setExtendedState(MAXIMIZED_BOTH);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tabelaSeguro = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        strc = new javax.swing.JRadioButton();
        busca = new javax.swing.JTextField();
        pesquisaSeguro = new javax.swing.JButton();
        valor = new javax.swing.JRadioButton();
        jButton3 = new javax.swing.JButton();
        Remetente = new javax.swing.JRadioButton();
        Destinatario = new javax.swing.JRadioButton();
        Cliente = new javax.swing.JRadioButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Buscar seguro");

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Pesquisar seguro", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 14), new java.awt.Color(204, 0, 0))); // NOI18N

        tabelaSeguro.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        tabelaSeguro.addAncestorListener(new javax.swing.event.AncestorListener() {
            public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
            }
            public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
                tabelaSeguroAncestorAdded(evt);
            }
            public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
            }
        });
        jScrollPane2.setViewportView(tabelaSeguro);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icones/visualizar.png"))); // NOI18N
        jButton1.setText("Ver detalhes");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        strc.setBackground(new java.awt.Color(255, 255, 255));
        buttonGroup1.add(strc);
        strc.setText("STRC");
        strc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                strcActionPerformed(evt);
            }
        });

        pesquisaSeguro.setText("Pesquisar");
        pesquisaSeguro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pesquisaSeguroActionPerformed(evt);
            }
        });

        valor.setBackground(new java.awt.Color(255, 255, 255));
        buttonGroup1.add(valor);
        valor.setText("Valor Fatura");
        valor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                valorActionPerformed(evt);
            }
        });

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icones/voltar.png"))); // NOI18N
        jButton3.setText("Voltar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        Remetente.setBackground(new java.awt.Color(255, 255, 255));
        buttonGroup1.add(Remetente);
        Remetente.setText("Remetente");

        Destinatario.setBackground(new java.awt.Color(255, 255, 255));
        buttonGroup1.add(Destinatario);
        Destinatario.setText("Destinatario");

        Cliente.setBackground(new java.awt.Color(255, 255, 255));
        buttonGroup1.add(Cliente);
        Cliente.setText("Cliente");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(pesquisaSeguro)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addComponent(strc)
                                    .addGap(18, 18, 18)
                                    .addComponent(valor)
                                    .addGap(18, 18, 18)
                                    .addComponent(Remetente)
                                    .addGap(18, 18, 18)
                                    .addComponent(Destinatario))
                                .addComponent(busca, javax.swing.GroupLayout.PREFERRED_SIZE, 281, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(18, 18, 18)
                        .addComponent(Cliente))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 718, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(155, 155, 155))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                            .addComponent(jButton3)
                            .addGap(57, 57, 57)
                            .addComponent(jButton1)
                            .addGap(164, 164, 164))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(strc)
                    .addComponent(valor)
                    .addComponent(Remetente)
                    .addComponent(Destinatario)
                    .addComponent(Cliente))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(busca, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pesquisaSeguro)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, Short.MAX_VALUE)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addGap(16, 16, 16))
        );

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel1.setForeground(new java.awt.Color(153, 0, 0));
        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icones/pesquisararquivo.png"))); // NOI18N
        jLabel1.setText("Buscar seguro");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(79, 79, 79)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 804, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addContainerGap(125, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addComponent(jLabel1)
                .addGap(38, 38, 38)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(217, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-1024)/2, (screenSize.height-760)/2, 1024, 760);
    }// </editor-fold>//GEN-END:initComponents

    private void tabelaSeguroAncestorAdded(javax.swing.event.AncestorEvent evt) {//GEN-FIRST:event_tabelaSeguroAncestorAdded
     
        tabelaSeguro.setModel(tabela);
        tabela.addColumn("STRC");
        tabela.addColumn("REMETENTE");
        tabela.addColumn("DESTINATARIO");
        tabela.addColumn("MERCADORIA");
        tabela.addColumn("PLACA DO VEICULO");
        tabela.addColumn("FATURA");
        tabela.addColumn("VALOR TOTAL");
        tabela.addColumn("VALOR DA FATURA");
}//GEN-LAST:event_tabelaSeguroAncestorAdded

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if(tabelaSeguro.getSelectedRow()!= -1){
            VisualizarSeguro visualizarSeguro = new VisualizarSeguro(cli.get(tabelaSeguro.getSelectedRow()));
            visualizarSeguro.setVisible(true);
            this.dispose();
        }else{
            JOptionPane.showMessageDialog(null, "Escolha um seguro");
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void pesquisaSeguroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pesquisaSeguroActionPerformed
          final Progress p = new Progress();
          p.setVisible(true);
          SwingWorker worker = new SwingWorker(){
               @Override
               protected Object doInBackground() throws Exception {
                     preenche(); // meu processo demorado
                    return null;
               }
               @Override
               protected void done() {
                    p.setVisible(false);
               }
          };
          worker.execute();
    }//GEN-LAST:event_pesquisaSeguroActionPerformed

    public void preenche(){
        cli.clear();
       TelaMetodos.RemoveDadosTabela(tabela, tabelaSeguro);
        cli.addAll(Controlador.buscarSeguros(busca.getText(), selecao()));
       if(cli !=null){
             for(Cliente_has_Seguro c:cli){
                 TelaMetodos.colocarDados(tabela,c.getCli(),c.getSeg(),c.getValorFatura(),tabelaSeguro.getColumnCount());
           }
        }
    }
  


    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
            TelaMetodos.Dispose(this);

    }//GEN-LAST:event_jButton3ActionPerformed

    private void strcActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_strcActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_strcActionPerformed

    private void valorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_valorActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_valorActionPerformed
  
    public int selecao(){
        if(strc.isSelected())
            return 1;
        else if(valor.isSelected())
            return 2;
        else if(Remetente.isSelected())
            return 3;
        else if(Destinatario.isSelected())
            return 4;
        else if(Cliente.isSelected())
            return 5;
        return 1;
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new BuscaSeguroVisualizar().setVisible(true);
            }
        });
    }
   DefaultTableModel tabela = new DefaultTableModel();
   private ArrayList <Cliente_has_Seguro> cli = new ArrayList <Cliente_has_Seguro>();
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton Cliente;
    private javax.swing.JRadioButton Destinatario;
    private javax.swing.JRadioButton Remetente;
    private javax.swing.JTextField busca;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton pesquisaSeguro;
    private javax.swing.JRadioButton strc;
    private javax.swing.JTable tabelaSeguro;
    private javax.swing.JRadioButton valor;
    // End of variables declaration//GEN-END:variables

}
